<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物流名称" prop="shippingName">
        <el-input
          v-model="queryParams.shippingName"
          placeholder="请输入物流名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="买家昵称" prop="buyerNick">
        <el-input
          v-model="queryParams.buyerNick"
          placeholder="请输入买家昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="receiver">
        <el-input
          v-model="queryParams.receiver"
          placeholder="请输入收货人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家ID" prop="sellerId">
        <el-input
          v-model="queryParams.sellerId"
          placeholder="请输入商家ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manager:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manager:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manager:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manager:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="实付金额" align="center" prop="payment" />
      <el-table-column label="支付类型" align="center" prop="paymentType" />
      <el-table-column label="邮费" align="center" prop="postFee" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="付款时间" align="center" prop="paymentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="consignTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.consignTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易完成时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易关闭时间" align="center" prop="closeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.closeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物流名称" align="center" prop="shippingName" />
      <el-table-column label="物流单号" align="center" prop="shippingCode" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="买家留言" align="center" prop="buyerMessage" />
      <el-table-column label="买家昵称" align="center" prop="buyerNick" />
      <el-table-column label="买家是否已经评价" align="center" prop="buyerRate" />
      <el-table-column label="收货人地区名称(省，市，县)街道" align="center" prop="receiverAreaName" />
      <el-table-column label="收货人手机" align="center" prop="receiverMobile" />
      <el-table-column label="收货人邮编" align="center" prop="receiverZipCode" />
      <el-table-column label="收货人" align="center" prop="receiver" />
      <el-table-column label="过期时间，定期清理" align="center" prop="expire" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expire, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发票类型(普通发票，电子发票，增值税发票)" align="center" prop="invoiceType" />
      <el-table-column label="订单来源：1:app端，2：pc端，3：M端，4：微信端，5：手机qq端" align="center" prop="sourceType" />
      <el-table-column label="商家ID" align="center" prop="sellerId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manager:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manager:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="实付金额" prop="payment">
          <el-input v-model="form.payment" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="邮费" prop="postFee">
          <el-input v-model="form.postFee" placeholder="请输入邮费" />
        </el-form-item>
        <el-form-item label="付款时间" prop="paymentTime">
          <el-date-picker clearable
            v-model="form.paymentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择付款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货时间" prop="consignTime">
          <el-date-picker clearable
            v-model="form.consignTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交易完成时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择交易完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交易关闭时间" prop="closeTime">
          <el-date-picker clearable
            v-model="form.closeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择交易关闭时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="物流名称" prop="shippingName">
          <el-input v-model="form.shippingName" placeholder="请输入物流名称" />
        </el-form-item>
        <el-form-item label="物流单号" prop="shippingCode">
          <el-input v-model="form.shippingCode" placeholder="请输入物流单号" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="买家留言" prop="buyerMessage">
          <el-input v-model="form.buyerMessage" placeholder="请输入买家留言" />
        </el-form-item>
        <el-form-item label="买家昵称" prop="buyerNick">
          <el-input v-model="form.buyerNick" placeholder="请输入买家昵称" />
        </el-form-item>
        <el-form-item label="买家是否已经评价" prop="buyerRate">
          <el-input v-model="form.buyerRate" placeholder="请输入买家是否已经评价" />
        </el-form-item>
        <el-form-item label="收货人地区名称(省，市，县)街道" prop="receiverAreaName">
          <el-input v-model="form.receiverAreaName" placeholder="请输入收货人地区名称(省，市，县)街道" />
        </el-form-item>
        <el-form-item label="收货人手机" prop="receiverMobile">
          <el-input v-model="form.receiverMobile" placeholder="请输入收货人手机" />
        </el-form-item>
        <el-form-item label="收货人邮编" prop="receiverZipCode">
          <el-input v-model="form.receiverZipCode" placeholder="请输入收货人邮编" />
        </el-form-item>
        <el-form-item label="收货人" prop="receiver">
          <el-input v-model="form.receiver" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="过期时间，定期清理" prop="expire">
          <el-date-picker clearable
            v-model="form.expire"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择过期时间，定期清理">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商家ID" prop="sellerId">
          <el-input v-model="form.sellerId" placeholder="请输入商家ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/manager/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单列表表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paymentType: null,
        status: null,
        shippingName: null,
        userId: null,
        buyerNick: null,
        receiver: null,
        sellerId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        payment: null,
        paymentType: null,
        postFee: null,
        status: null,
        createTime: null,
        updateTime: null,
        paymentTime: null,
        consignTime: null,
        endTime: null,
        closeTime: null,
        shippingName: null,
        shippingCode: null,
        userId: null,
        buyerMessage: null,
        buyerNick: null,
        buyerRate: null,
        receiverAreaName: null,
        receiverMobile: null,
        receiverZipCode: null,
        receiver: null,
        expire: null,
        invoiceType: null,
        sourceType: null,
        sellerId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单列表编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manager/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
